
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="NodeJS,Npm," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>创建npm插件 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        创建npm插件
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/NodeJS/" rel="tag">NodeJS</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Npm/" rel="tag">Npm</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 4 分钟</span>
    </header>

    <div class="post-content">
      <h6 id="在github创建一个项目，比如创建名为randomNickname的项目，"><a href="#在github创建一个项目，比如创建名为randomNickname的项目，" class="headerlink" title="在github创建一个项目，比如创建名为randomNickname的项目，"></a>在<a target="_blank" rel="noopener" href="https://github.com/">github</a>创建一个项目，比如创建名为<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/randomNickname">randomNickname</a>的项目，</h6><p>这个代码库用户存放npm插件的代码。</p>
<h6 id="首先在npm官网申请账号，后面提交到npm官网需要用到。"><a href="#首先在npm官网申请账号，后面提交到npm官网需要用到。" class="headerlink" title="首先在npm官网申请账号，后面提交到npm官网需要用到。"></a>首先在npm<a target="_blank" rel="noopener" href="https://www.npmjs.com/">官网</a>申请账号，后面提交到npm官网需要用到。</h6><h6 id="mkdir-npmjs"><a href="#mkdir-npmjs" class="headerlink" title="mkdir npmjs"></a><code>mkdir npmjs</code></h6><h6 id="cd-npmjs"><a href="#cd-npmjs" class="headerlink" title="cd npmjs"></a><code>cd npmjs</code></h6><h6 id="npm-init"><a href="#npm-init" class="headerlink" title="npm init"></a><code>npm init</code></h6><h6 id="根据提示输入相应的信息，可用参考如下设置。"><a href="#根据提示输入相应的信息，可用参考如下设置。" class="headerlink" title="根据提示输入相应的信息，可用参考如下设置。"></a>根据提示输入相应的信息，可用参考如下设置。</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$ npm init</span><br><span class="line">This utility will walk you through creating a package.json file.</span><br><span class="line">It only covers the most common items, and tries to guess sensible defaults.</span><br><span class="line"></span><br><span class="line">See `npm help json` for definitive documentation on these fields</span><br><span class="line">and exactly what they do.</span><br><span class="line"></span><br><span class="line">Use `npm install &lt;pkg&gt; --save` afterwards to install a package and</span><br><span class="line">save it as a dependency in the package.json file.</span><br><span class="line"></span><br><span class="line">Press ^C at any time to quit.</span><br><span class="line">name: (randomNichname)</span><br><span class="line">Sorry, name can no longer contain capital letters.</span><br><span class="line">name: (randomNichname) randomNichname</span><br><span class="line">Sorry, name can no longer contain capital letters.</span><br><span class="line">name: (randomNichname) y</span><br><span class="line">version: (1.0.0) 0.1.0</span><br><span class="line">description: Get the name of the three kingdoms.</span><br><span class="line">entry point: (index.js) y</span><br><span class="line">test command:</span><br><span class="line">git repository: https://github.com/ubuntuvim/randomNichname.git</span><br><span class="line">keywords: nickname,random</span><br><span class="line">author: ubuntuvim</span><br><span class="line">license: (ISC) ISC</span><br><span class="line">About to write to /Users/ubuntuvim/codes/my-npm-plugins/randomNichname/package.json:</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  &quot;name&quot;: &quot;y&quot;,</span><br><span class="line">  &quot;version&quot;: &quot;0.1.0&quot;,</span><br><span class="line">  &quot;description&quot;: &quot;Get the name of the three kingdoms.&quot;,</span><br><span class="line">  &quot;main&quot;: &quot;y&quot;,</span><br><span class="line">  &quot;scripts&quot;: &#123;</span><br><span class="line">    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;repository&quot;: &#123;</span><br><span class="line">    &quot;type&quot;: &quot;git&quot;,</span><br><span class="line">    &quot;url&quot;: &quot;git+https://github.com/ubuntuvim/randomNichname.git&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;keywords&quot;: [</span><br><span class="line">    &quot;nickname&quot;,</span><br><span class="line">    &quot;random&quot;</span><br><span class="line">  ],</span><br><span class="line">  &quot;author&quot;: &quot;ubuntuvim&quot;,</span><br><span class="line">  &quot;license&quot;: &quot;ISC&quot;,</span><br><span class="line">  &quot;bugs&quot;: &#123;</span><br><span class="line">    &quot;url&quot;: &quot;https://github.com/ubuntuvim/randomNichname/issues&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;homepage&quot;: &quot;https://github.com/ubuntuvim/randomNichname#readme&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Is this ok? (yes) yes</span><br><span class="line">ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$</span><br></pre></td></tr></table></figure>
<p>创建完成package.json之后你也可以根据实际情况做修改。</p>
<h6 id="创建npm插件所需的目录和文件，目录结构如下："><a href="#创建npm插件所需的目录和文件，目录结构如下：" class="headerlink" title="创建npm插件所需的目录和文件，目录结构如下："></a>创建npm插件所需的目录和文件，目录结构如下：</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">npmjs</span><br><span class="line">├─┬ lib</span><br><span class="line">│ └── npmjs.js</span><br><span class="line">├─┬ test</span><br><span class="line">│ └── test.js</span><br><span class="line">├── .gitignore</span><br><span class="line">├── .npmignore</span><br><span class="line">├── .travis.yml</span><br><span class="line">├── index.js</span><br><span class="line">├── LICENSE</span><br><span class="line">├── makefile</span><br><span class="line">├── package.json</span><br><span class="line">├── README.md</span><br></pre></td></tr></table></figure>
<p>这些文件的作用是：</p>
<ul>
<li>lib目录下存放业务逻辑文件</li>
<li>test目录下存放单元测试用例</li>
<li>.npmignore记录哪些文件不需要被发布到npmjs.org</li>
<li>.travis.yml是持续集成服务travis的描述文件</li>
<li>index.js是入口文件</li>
<li>makefile方便我们用make test进行测试</li>
<li>README.md是此module的描述和使用方法</li>
</ul>
<h6 id="编写插件代码"><a href="#编写插件代码" class="headerlink" title="编写插件代码"></a>编写插件代码</h6><p>主要代码直接放在<code>index.js</code>。比如本插件是用于获取随机的三国人物名称，代码如下(详细代码请从<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/randomNickname/blob/master/index.js">github</a>下载)：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  从1099个名字中获取任意名字</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="meta">  &#x27;use strict&#x27;</span>;</span><br><span class="line">    <span class="comment">//  一共1099个</span></span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="comment">//  获取随机名字</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getNickname</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  取0~1099的随机数</span></span><br><span class="line">        <span class="keyword">var</span> random = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">1099</span>);</span><br><span class="line">        <span class="keyword">if</span> (random &gt;= <span class="number">1099</span>)</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;获取人名数组下标月结！&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> arr[random];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">exports</span>.getNickname = getNickname;</span><br><span class="line">  &#125;());</span><br></pre></td></tr></table></figure>
<h6 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h6><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="built_in">require</span>(<span class="string">&#x27;../index&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> s = arr.getNickname();</span><br><span class="line"><span class="keyword">if</span> (s) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(s);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;The test does not pass...&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行测试。<br><code>node ./test/test.js</code></p>
<h6 id="在LICENSE文件中添加许可信息"><a href="#在LICENSE文件中添加许可信息" class="headerlink" title="在LICENSE文件中添加许可信息"></a>在LICENSE文件中添加许可信息</h6><p>许可内容请从<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/randomNickname/blob/master/LICENSE">github</a>下载。</p>
<h6 id="编写使用文档README-md"><a href="#编写使用文档README-md" class="headerlink" title="编写使用文档README.md"></a>编写使用文档README.md</h6><p><strong>插件使用方式</strong></p>
<ol>
<li><p>安装插件<br><code>npm install randomNickname</code></p>
</li>
<li><p>使用插件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> names = <span class="built_in">require</span>(<span class="string">&#x27;randomNickname&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> s = names.getNickname();</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;得到的人物名字为： &#x27;</span> + s);</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h6 id="提交代码到github"><a href="#提交代码到github" class="headerlink" title="提交代码到github"></a>提交代码到github</h6><p>步骤如下：</p>
<ul>
<li><code>git init</code></li>
<li><code>git remote add origin &lt;git远程URL&gt;</code></li>
<li><code>git add *</code></li>
<li><code>git commit -am &#39;描述信息&#39;</code></li>
<li><code>git push origin master</code><br><em>如果出现类似如下错误</em></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">! [rejected]        master -&gt; master (non-fast-forward)</span><br><span class="line">error: failed to push some refs to &#x27;https://github.com/ubuntuvim/randomNickname.git&#x27;</span><br><span class="line">hint: Updates were rejected because the tip of your current branch is behind</span><br><span class="line">hint: its remote counterpart. Integrate the remote changes (e.g.</span><br><span class="line">hint: &#x27;git pull ...&#x27;) before pushing again.</span><br><span class="line">hint: See the &#x27;Note about fast-forwards&#x27; in &#x27;git push --help&#x27; for details.</span><br></pre></td></tr></table></figure>

<p>先更新合并再提交到远程代码库。</p>
<ul>
<li><code>git pull</code></li>
<li><code>git merge origin/master</code></li>
<li><code>git push origin/master</code></li>
</ul>
<h6 id="发布到npmjs官网"><a href="#发布到npmjs官网" class="headerlink" title="发布到npmjs官网"></a>发布到npmjs官网</h6><p>步骤如下：</p>
<ul>
<li><code>npm adduser</code>   (输入你在npmjs官网注册的账号和密码)</li>
<li><code>npm publish .</code></li>
</ul>
<p>如果出现错误：<br><code>no_perms Private mode enable, only admin can publish this module</code><br>执行下列代码重置后再执行<code>npm adduser</code>、<code>npm publish .</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm config set registry http:&#x2F;&#x2F;registry.npmjs.org</span><br></pre></td></tr></table></figure>

<p>等待完成，看到如下信息说明发布成功。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">+ randomnickname@0.1.0</span><br></pre></td></tr></table></figure>

<p><em>如果你的代码修改了又需要重新发布到npmjs上，则首先修改<code>package.json</code>里的版本号，再执行<code>npm publish .</code>即可。</em></p>
<p>此时你可以到npmjs的个人中心中查看刚刚发布的插件。<br>比如我的个人中心<a target="_blank" rel="noopener" href="https://www.npmjs.com/~ubuntuvim">https://www.npmjs.com/~ubuntuvim</a>。</p>
<p><strong>参考文献：</strong></p>
<ol>
<li><a target="_blank" rel="noopener" href="http://segmentfault.com/a/1190000000491188">http://segmentfault.com/a/1190000000491188</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.csdn.net/liuxiao723846/article/details/46237289">http://blog.csdn.net/liuxiao723846/article/details/46237289</a></li>
<li><a target="_blank" rel="noopener" href="http://www.lellansin.com/npm-publish-%E5%8F%91%E5%B8%83%E7%A4%BA%E4%BE%8B.html">http://www.lellansin.com/npm-publish-%E5%8F%91%E5%B8%83%E7%A4%BA%E4%BE%8B.html</a></li>
</ol>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/Ruby/SSL%E8%AF%81%E4%B9%A6%E9%97%AE%E9%A2%98/" rel="next" title="SSL证书问题">
          SSL证书问题
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/Mac/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E9%85%8D%E7%BD%AE%E9%94%99%E8%AF%AF%E6%97%B6%E4%B8%B4%E6%97%B6%E8%A7%A3%E6%95%91%E5%8A%9E%E6%B3%95/" rel="prev" title="环境变量配置错误时临时解救办法">
            环境变量配置错误时临时解救办法
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%9C%A8github%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE%EF%BC%8C%E6%AF%94%E5%A6%82%E5%88%9B%E5%BB%BA%E5%90%8D%E4%B8%BArandomNickname%E7%9A%84%E9%A1%B9%E7%9B%AE%EF%BC%8C"><span class="toc-text">在github创建一个项目，比如创建名为randomNickname的项目，</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%A6%96%E5%85%88%E5%9C%A8npm%E5%AE%98%E7%BD%91%E7%94%B3%E8%AF%B7%E8%B4%A6%E5%8F%B7%EF%BC%8C%E5%90%8E%E9%9D%A2%E6%8F%90%E4%BA%A4%E5%88%B0npm%E5%AE%98%E7%BD%91%E9%9C%80%E8%A6%81%E7%94%A8%E5%88%B0%E3%80%82"><span class="toc-text">首先在npm官网申请账号，后面提交到npm官网需要用到。</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#mkdir-npmjs"><span class="toc-text">mkdir npmjs</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#cd-npmjs"><span class="toc-text">cd npmjs</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#npm-init"><span class="toc-text">npm init</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%A0%B9%E6%8D%AE%E6%8F%90%E7%A4%BA%E8%BE%93%E5%85%A5%E7%9B%B8%E5%BA%94%E7%9A%84%E4%BF%A1%E6%81%AF%EF%BC%8C%E5%8F%AF%E7%94%A8%E5%8F%82%E8%80%83%E5%A6%82%E4%B8%8B%E8%AE%BE%E7%BD%AE%E3%80%82"><span class="toc-text">根据提示输入相应的信息，可用参考如下设置。</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAnpm%E6%8F%92%E4%BB%B6%E6%89%80%E9%9C%80%E7%9A%84%E7%9B%AE%E5%BD%95%E5%92%8C%E6%96%87%E4%BB%B6%EF%BC%8C%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%E5%A6%82%E4%B8%8B%EF%BC%9A"><span class="toc-text">创建npm插件所需的目录和文件，目录结构如下：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E6%8F%92%E4%BB%B6%E4%BB%A3%E7%A0%81"><span class="toc-text">编写插件代码</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-text">测试</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%9C%A8LICENSE%E6%96%87%E4%BB%B6%E4%B8%AD%E6%B7%BB%E5%8A%A0%E8%AE%B8%E5%8F%AF%E4%BF%A1%E6%81%AF"><span class="toc-text">在LICENSE文件中添加许可信息</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3README-md"><span class="toc-text">编写使用文档README.md</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%8F%90%E4%BA%A4%E4%BB%A3%E7%A0%81%E5%88%B0github"><span class="toc-text">提交代码到github</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%8F%91%E5%B8%83%E5%88%B0npmjs%E5%AE%98%E7%BD%91"><span class="toc-text">发布到npmjs官网</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '创建npm插件', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
